package org.itsolutions.javaBackup.utils;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;


/**
 * Container class for any two object.
 * @param <E> first element in tuple
 * @param <F> second element in tuple
 * @author Tomas Fecko
 */
public final class DoubleTuple<E, F> implements Externalizable {
	
	private E mFirstElement;
	private F mSecondElement;

	/**
	 * 
	 */
	public DoubleTuple() {
	}

	/**
	 * 
	 * @param pFirstElement first element in tuple
	 * @param pSecondElement second element in tuple
	 */
	public DoubleTuple(E pFirstElement, F pSecondElement) {
		mFirstElement = pFirstElement;
		mSecondElement = pSecondElement;
	}
	
	/**
	 * @return the pFirstElement
	 */
	public E getFirstElement() {
		return mFirstElement;
	}

	/**
	 * @return the pSecondElement
	 */
	public F getSecondElement() {
		return mSecondElement;
	}

	@Override
	public String toString() {
		return "DoubleTuple [mFirstElement=" + mFirstElement + ", mSecondElement=" + mSecondElement + "]";
	}

	@Override
	public void writeExternal(ObjectOutput pOut) throws IOException {
		pOut.writeObject(mFirstElement);
		pOut.writeObject(mSecondElement);
	}

	@Override
	@SuppressWarnings("unchecked")
	public void readExternal(ObjectInput pIn) throws IOException, ClassNotFoundException {
		mFirstElement = (E) pIn.readObject();
		mSecondElement = (F) pIn.readObject(); 
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((mFirstElement == null) ? 0 : mFirstElement.hashCode());
		result = prime * result + ((mSecondElement == null) ? 0 : mSecondElement.hashCode());
		return result;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public boolean equals(Object pObj) {
		if (this == pObj) {
			return true;
		}
		if (pObj == null) {
			return false;
		}
		if (!(pObj instanceof DoubleTuple)) {
			return false;
		}
		DoubleTuple other = (DoubleTuple) pObj;
		if (mFirstElement == null) {
			if (other.mFirstElement != null) {
				return false;
			}
		} else if (!mFirstElement.equals(other.mFirstElement)) {
			return false;
		}
		if (mSecondElement == null) {
			if (other.mSecondElement != null) {
				return false;
			}
		} else if (!mSecondElement.equals(other.mSecondElement)) {
			return false;
		}
		return true;
	}
}





